package com.andova.camera

import android.content.Intent
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.andova.cameraview.AspectRatio
import com.andova.cameraview.CameraView
import com.andova.cameraview.Size
import com.andova.ext.e
import com.andova.ext.printCounter
import com.andova.opengles.R
import com.andova.opengles.databinding.AcCameraViewBinding
import kotlin.math.max

class CameraViewActivity : AppCompatActivity() {
    private lateinit var mBinding: AcCameraViewBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = AcCameraViewBinding.inflate(layoutInflater)
        setContentView(mBinding.root)
    }

    override fun onResume() {
        super.onResume()
        mBinding.backcamera.setFacing(CameraView.FACING_BACK, true)
        mBinding.backcamera.setAspectRatio(AspectRatio.of(3, 4))
        mBinding.backcamera.addCallback(object : CameraView.Callback() {
            override fun onPreviewFrame(cameraView: CameraView?, data: ByteArray?) {
                data ?: return
                printCounter()
//                i("back camera onPreviewFrame ${data?.size}")
            }

            override fun adjustPreviewSize(sizes: List<Size>?): Size? {
                e("back camera adjustPreviewSize")
                var pixels = 0
                val filters = mutableListOf<Size>()
                for (size in sizes ?: return null) {
                    if (size.height < size.width) continue
                    filters.add(size)
                    pixels = max(pixels, size.width * size.height)
                }
                for (size in filters) {
                    if (size.width == 480 && size.height == 640) {
                        return size
                    }
//                    return size
//                    if (size.width * size.height >= pixels) return size
                }
                return null
            }

            override fun adjustPictureSize(sizes: List<Size>?): Size? {
                e("back camera adjustPictureSize")
                var pixels = 0
                val filters = mutableListOf<Size>()
                for (size in sizes ?: return null) {
                    if (size.height < size.width) continue
                    filters.add(size)
                    pixels = max(pixels, size.width * size.height)
                }
                for (size in filters) {
                    if (size.width == 480 && size.height == 640) {
                        return size
                    }
//                    return size
//                    if (size.width * size.height >= pixels) return size
                }
                return null
            }
        })
        mBinding.backcamera.start(true, 1)

        mBinding.frontcamera.setFacing(CameraView.FACING_FRONT, true)
        mBinding.frontcamera.setAspectRatio(AspectRatio.of(3, 4))
        mBinding.frontcamera.addCallback(object : CameraView.Callback() {
            override fun onPreviewFrame(cameraView: CameraView?, data: ByteArray?) {
                data ?: return
                printCounter()
//                i("front camera onPreviewFrame ${data?.size}")
            }

            override fun adjustPreviewSize(sizes: List<Size>?): Size? {
                e("front camera adjustPreviewSize")
                var pixels = 0
                val filters = mutableListOf<Size>()
                for (size in sizes ?: return null) {
                    if (size.height < size.width) continue
                    filters.add(size)
                    pixels = max(pixels, size.width * size.height)
                }
                for (size in filters) {
                    if (size.width == 480 && size.height == 640) {
                        return size
                    }
//                    return size
//                    if (size.width * size.height >= pixels) return size
                }
                return null
            }

            override fun adjustPictureSize(sizes: List<Size>?): Size? {
                e("front camera adjustPictureSize")
                var pixels = 0
                val filters = mutableListOf<Size>()
                for (size in sizes ?: return null) {
                    if (size.height < size.width) continue
                    filters.add(size)
                    pixels = max(pixels, size.width * size.height)
                }
                for (size in filters) {
                    if (size.width == 480 && size.height == 640) {
                        return size
                    }
//                    return size
//                    if (size.width * size.height >= pixels) return size
                }
                return null
            }
        })
        mBinding.frontcamera.start(true, 1)
    }

    override fun onPause() {
        super.onPause()
        mBinding.backcamera.stop()
        mBinding.frontcamera.stop()
    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        if (!hasFocus) return
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_FULLSCREEN
                or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        when (event?.action) {
            MotionEvent.ACTION_UP -> startActivity(Intent(this, EmptyActivity::class.java))
        }
        return super.onTouchEvent(event)
    }
}